package com.jianyun.jyzs.utils;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.jianyun.jyzs.ThisApp;
import com.jrmf360.rylib.common.util.ToastUtil;

import com.jianyun.jyzs.utils.RceLog;


/**
 * Created by mlli on 2017/11/13.
 */

public class LocationManager {

    private  Activity context;

    private AMapLocationClient locationClient;
    private AMapLocationClientOption defaultOption;
    private AMapLocation aMapLocation = null;
    private LocationManagerListener managerListener;
    //String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    private static final int PERMISSON_REQUESTCODE = 0;

    /**
     * 需要进行检测的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    public LocationManager(Activity context) {
        this.context = context;
        initLocation(context);
    }

    public void getaMapLocation(GetaMaplocation getaMaplocation){
        this.getaMaplocation = getaMaplocation;
        if (aMapLocation==null){
            if(!PermissionCheckUtil.checkPermissions(context, needPermissions)) {
                PermissionCheckUtil.requestPermissions(context, needPermissions, 100);
            }else {
                locationClient.startLocation();
            }
        }else {
            getaMaplocation.getaMapResult(aMapLocation);
        }
    }
    public void locationStart(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkSelfPermission = context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            if(checkSelfPermission!= PackageManager.PERMISSION_GRANTED){
                //没有权限 请求
                context.requestPermissions(needPermissions,PERMISSON_REQUESTCODE);
            }else {
                locationClient.startLocation();
            }
        }else {
            locationClient.startLocation();
        }
    }

    public void locationStart(LocationManagerListener locationManagerListener){
        this.managerListener = locationManagerListener;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkSelfPermission = context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            if(checkSelfPermission!= PackageManager.PERMISSION_GRANTED){
                //没有权限 请求
                context.requestPermissions(needPermissions,PERMISSON_REQUESTCODE);
            }else {
                locationClient.startLocation();
            }
        }else {
            locationClient.startLocation();
        }
    }
    public void locationStart(ProgressDialog dialog, LocationManagerListener locationManagerListener){
        this.managerListener = locationManagerListener;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkSelfPermission = context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            if(checkSelfPermission!= PackageManager.PERMISSION_GRANTED){
                //没有权限 请求
                context.requestPermissions(needPermissions,PERMISSON_REQUESTCODE);
                RceLog.d("lml","请求权限");
            }else {
                dialog.show();
//                ToastUtil.showNoWaitToast(context,"开始定位");
                if (dialog!=null)dialog.setMessage("开始定位");
                locationClient.startLocation();
            }
        }else {
            if (dialog!=null)dialog.show();
            if (dialog!=null)dialog.setMessage("开始定位");
//            ToastUtil.showNoWaitToast(context,"开始定位");
            locationClient.startLocation();
        }
    }
    /**
     * 定位监听
     */
    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            if (null != location) {
                if (location.getErrorCode() == 0) {
                    aMapLocation = location;
                    Log.i("test","定位成功："+location.toString());
                    //ToastUtil.showToast(context, "定位成功"+location.toString());
                    ThisApp.aMapLocation = aMapLocation;
                    if (managerListener!=null)managerListener.locationResult(true, aMapLocation);
                } else {
                    RceLog.d("lml",location.getErrorInfo());
                    if (managerListener!=null)managerListener.locationResult(false, aMapLocation);
                }
            }else {
                ToastUtil.showToast(context, "定位失败");
                if (managerListener!=null)managerListener.locationResult(false, location);
            }
        }

    };



    public interface LocationManagerListener {
        void locationResult(boolean result, AMapLocation aMapLocation);
    }
    public interface GetaMaplocation{
        void getaMapResult(AMapLocation aMapLocation);
    }
    private GetaMaplocation getaMaplocation;

    private void initLocation(Context context) {
        locationClient = new AMapLocationClient(context);
        defaultOption = getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(defaultOption);
        // 设置定位监听
        locationClient.setLocationListener(locationListener);

    }



    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }
}
